<template>
  <j-modal title="服药录入" :width="900" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol"
      style="padding:0 0 20px 20px;">
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="病人编号">
            <a-input v-model="form.patientId" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="姓名" required prop="realName">
            <a-input v-model="form.realName" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="身份证号码" prop="identityCard">
            <a-input v-model="form.identityCard" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="服药次数" prop="takeCureCount">
            <a-input v-model="form.takeCureCount" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="服药日期" prop="takeMedicineTime">
            <a-date-picker allowClear v-model="form.takeMedicineTime" valueFormat="YYYY-MM-DD"
              @change="rangePickerChange" style="width:100%" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="药物批号" prop="cureId">
            <a-input v-model="form.cureId" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="当前剂量" prop="dose">
            <a-input-number v-model="form.dose" placeholder="请输入" style="width:93%"> </a-input-number> mg
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="备选药物批号">
            <a-input v-model="form.spareCureId" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="备选当前剂量">
            <a-input-number v-model="form.spareDose" placeholder="请输入" style="width:93%"> </a-input-number> mg
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="医生编号" prop="doctorId">
            <a-input v-model="form.doctorId" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as treatApi from '@/api/sjsk/help/treat.js'
export default {
  props: ['title'],
  components: {},
  data() {
    return {
      labelCol: { span: 22 },
      wrapperCol: { span: 22 },
      form: {},
      rules: {
        realName: [{ required: true, message: '请输入' }],
        identityCard: [{ required: true, message: '请输入' }],
        patientId: [{ required: true, message: '请输入' }],
        name: [{ required: true, message: '请输入' }],
        takeCureCount: [{ required: true, message: '请输入' }],
        takeMedicineTime: [{ required: true, message: '请选择' }],
        cureId: [{ required: true, message: '请输入' }],
        dose: [{ required: true, message: '请输入' }],
        // spareCureId: [{ required: true, message: '请输入' }],
        // spareDose: [{ required: true, message: '请输入' }],
        doctorId: [{ required: true, message: '请输入' }]
      },
      isDisabled: true,
      visible: false,
      loading: false
    }
  },
  created() {
    // let user = Vue.ls.get(USER_INFO)
    // this.form.apprehendUser = Vue.ls.get(USER_INFO).realname
  },
  methods: {
    show(patientId) {
      this.form = {}
      this.visible = true
      treatApi.getTakeCureInfoByPid({ pid: patientId }).then(res => {
        if (res.success) {
          const record = res.result ? res.result : null;
          this.$set(this.form, 'patientId', record.patientId)
          this.$set(this.form, 'realName', record.realName)
          this.$set(this.form, 'identityCard', record.identityCard)
          this.$set(this.form, 'takeCureCount', record.takeCureCount)
          this.$set(this.form, 'clinicId', record.clinicId)
          this.$set(this.form, 'clinicName', record.clinicName)
          this.$set(this.form, 'clinicRegion', record.clinicRegion)
        } else {
          this.$message.warn(res.message)
        }
      }).catch((e) => {
        this.loading = false
        this.$message.warn('请求失败！')
      })


      // treatApi.getPatientById({ patientId: id }).then(res => {
      //   if (res.success) {
      //     this.$set(this.form,'patientId',res.result.patientId)
      //     this.$set(this.form,'realName',res.result.ryName)
      //     this.$set(this.form,'identityCard',res.result.cardId)
      //     this.$set(this.form,'takeCureCount',res.result.takeCureCount)
      //     // this.form.patientId = res.result.patientId
      //     // this.form.ryName = res.result.ryName
      //     // this.form.cardId = res.result.cardId
      //     // this.form.takeCureCount = res.result.takeCureCount
      //   }
      // })
    },
    rangePickerChange(date) {
      // this.$set(this.form, 'startTime', date[0])
      // this.$set(this.form, 'endTime', date[1])
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          treatApi.addCureRecordById(this.form).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
/deep/.ant-col-22 {
  text-align: left;
}

/deep/.ant-row {
  margin-bottom: 0;
}
</style>
